package com.ld.security.auth.server.modules.ge.api;

import com.ld.security.auth.server.modules.ge.constant.GeModelConstant;
import com.ld.security.auth.server.modules.ge.entity.GeModel;
import com.ld.security.auth.server.modules.ge.po.GenteratorCodePo;
import com.ld.security.auth.server.modules.ge.po.GetAllModelDetailsPo;
import com.ld.security.auth.server.modules.ge.po.UpdateModelPo;
import com.ld.security.auth.server.modules.ge.service.IGeModelService;
import com.ld.security.auth.server.modules.ge.vo.ModelDetailVo;
import com.ld.security.common.core.base.vo.DataVo;
import com.ld.security.common.core.base.vo.ListVo;
import com.ld.security.common.core.base.vo.SuperVo;
import com.ld.security.common.core.util.ReflectionUtil;
import com.ld.security.common.mybatis.base.po.IdPo;
import com.ld.security.common.mybatis.base.po.IdsPo;
import com.ld.security.common.shiro.util.RespUtil;
import com.ld.security.common.tomcat.base.api.SuperApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@Api(tags="模型Api管理")
@RequestMapping("/api/geModel")
public class  GeModelApi extends  SuperApi<GeModel> {

    @Autowired
    private IGeModelService iGeModelService;

    String not_id="-1";

    @ApiOperation("获得模型详情")
    @RequestMapping(value = "/getDetails",method = RequestMethod.POST)
    public DataVo<ModelDetailVo> getDetails(@RequestBody IdPo idPo){
        ModelDetailVo vo;
        if(not_id.equals(idPo.getId())){
            //id不存在判断创建模型
            GeModel geModel = new GeModel();
            ReflectionUtil.fill(geModel);
            geModel.setModelType(GeModelConstant.modelType.mod);
            geModel.setTableSchema("model");
            vo = new ModelDetailVo();
            vo.setModel(geModel);
            vo.setFields(Arrays.asList());
        }else{

            IdsPo po =   new IdsPo(Arrays.asList(idPo.getId()));
            vo  = iGeModelService.getAllModelDetails(po).get(0);
        }
        return DataVo.of(vo);
    }


    @ApiOperation("获得所有模型详情")
    @RequestMapping(value = "/getAllModelDetails",method = RequestMethod.POST)
    public DataVo<ListVo<ModelDetailVo>> getAllModelDetails(){
        GetAllModelDetailsPo po = new GetAllModelDetailsPo();
        po.setTableType(GeModelConstant.tableType.anon);
        List<ModelDetailVo> vo = iGeModelService.getAllModelDetails(po);
        return DataVo.of( ListVo.of(vo));
    }

    @RequestMapping(value="/updateModel",method = RequestMethod.POST)
    @ApiOperation("更新一个模型")
    public SuperVo updateModel(@RequestBody UpdateModelPo po){
        iGeModelService.updateModel(po);
        DataVo vo = DataVo.of();
        vo.setMessage("更新模型成功");
        return vo;
    }

    @RequestMapping(value="/genteratorCode",method = RequestMethod.GET)
    @ApiOperation("代码生成")

    public void genteratorCode(GenteratorCodePo po){

        byte[] bs =  iGeModelService.genteratorCode(po);

        RespUtil.downloadFile("code.zip",bs);
    }





}
